package com.lm.jsch.example;

import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;

public class JSchSFTPUploadExample {
    public static void main(String[] args) {
        String host = "your-server-ip";
        String user = "your-username";
        String password = "your-password";
        String localFile = "/path/to/local/file.txt";
        String remoteDir = "/path/to/remote/dir/";

        try {
            JSch jsch = new JSch();
            Session session = jsch.getSession(user, host, 22);
            session.setPassword(password);

            // 忽略主机密钥检查
            session.setConfig("StrictHostKeyChecking", "no");
            session.connect();

            // 打开 SFTP 通道
            ChannelSftp channel = (ChannelSftp) session.openChannel("sftp");
            channel.connect();

            // 上传文件
            channel.put(localFile, remoteDir);
            System.out.println("File uploaded successfully!");

            channel.disconnect();
            session.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

